(function () {
  'use strict';

  var serviceId = 'datacontext';
  angular.module('ng4model').factory(serviceId, ['common', '$http', datacontext]);

  function datacontext(common, $http) {
    var $q = common.$q;

    var service = {
      getMessageCount: getMessageCount,
      getMessages: getMessages,

      getProjects: getProjects,
      addProject: addProject,
      addSampleProject: addSampleProject,
      getProjectsPublished: getProjectsPublished,
      getProject: getProject,
      getPublished: getPublished,

      getMembers: getMembers,
      getAllMembers: getAllMembers,
      getMember: getMember,
      getTeam: getTeam,

      getOpuses: getOpuses,
      getAllOpuses: getAllOpuses,
      getOpus: getOpus,
      addOpus: addOpus,
      addSection: addSection,
      delSection: delSection,
      updateSection: updateSection,

      getResources: getResources,
      getResource: getResource,
      getFile : getFile,
      deleteFile: deleteFile,
      uploadFile: uploadFile,
      addFolder: addFolder,
      emptyFolder: emptyFolder,
      emptyTrash: emptyTrash,
      
      getSemantics: getSemantics,
      addTerm: addTerm,
      deleteTerm: deleteTerm,
      changeTerm: changeTerm
    };

    return service;
    
    //** Messages

    function getMessageCount() {
      var promise = $http.get('/EasyAuthorPortal/api/GetMessageCount');
      return promise.then(function(data) { return data.data; });
    }
    
    function getMessages() {
      return $q.when(messages);
    }
    
    //** Projects

    function getProjects() {
      var promise = $http.get('/EasyAuthorPortal/api/GetProjects');
      return promise.then(function (data) { return data.data; });
    }
    
    function getProjectsPublished() {
      var promise = $http.get('/EasyAuthorPortal/api/GetProjectsPublished');
      return promise.then(function (data) { return data.data; });
    }

    function getPublished() {
      var promise = $http.get('/EasyAuthorPortal/api/GetProjects');
      return promise.then(function (data) { return data.data; });
    }

    function getProject(projectId) {
      var promise = $http.get('/EasyAuthorPortal/api/GetProject/' + projectId);
      return promise.then(function(data) { return data.data; });
    }

    function addSampleProject() {
      var promise = $http.post('/EasyAuthorPortal/api/AddSampleProject');
      return promise.then(function (data) { return data.data; });
    }

    function addProject(project) {
      var promise = $http.post('/EasyAuthorPortal/api/AddProject', project);
      return promise.then(function (data) { return data.data; });
    }


    //** Opus

    function getOpuses(projectId) {
      var promise = $http.get('/EasyAuthorPortal/api/GetOpuses/' + projectId);
      return promise.then(function(data) { return data.data; });
    }
    
    function getAllOpuses() {
      var promise = $http.get('/EasyAuthorPortal/api/GetAllOpuses/');
      return promise.then(function(data) { return data.data; });
    }

    function getOpus(opusId, withToc) {
      var promise = $http.get('/EasyAuthorPortal/api/GetOpus/' + opusId + '?withToc=True');
      return promise.then(function(data) { return data.data; });
    }

    function addOpus(pid) {
      
    }
    
    function addSection(opusId, newItem) {
      //newItem = { text: 'New Section', number: newNumber, items: [], parent: data, hasContent: false }
      var promise = $http.get('/EasyAuthorPortal/api/AddSection/' + opusId + '?&pid=' + newItem.parent.id + '&level=' + (newItem.number.split('.').length + 1) + '&content=' + newItem.text);
      return promise.then(function (data) { return data.data; });
    }
    
    function delSection(id) {
      var promise = $http.get('/EasyAuthorPortal/api/DelSection/' + id);
      return promise.then(function (data) { return data.data; });
    }
    
    function updateSection(id, content) {
      var promise = $http.get('/EasyAuthorPortal/api/UpdateSection/' + id + '?content=' + content);
      return promise.then(function (data) { return data.data; });
    }

    //** Teams & Members
    
    function getAllMembers() {
      var promise = $http.get('/EasyAuthorPortal/api/GetAllMembers/');
      return promise.then(function(data) { return data.data; });
    }

    function getMembers(teamId) {
      var promise = $http.get('/EasyAuthorPortal/api/GetMembers/' + teamId);
      return promise.then(function(data) { return data.data; });
    }
    
    function getMember(userId) {
      var promise = $http.get('/EasyAuthorPortal/api/GetMember/' + userId);
      return promise.then(function(data) { return data.data; });
    }

    function getTeam(teamId) {
      var promise = $http.get('/EasyAuthorPortal/api/GetTeam/' + teamId);
      return promise.then(function(data) { return data.data; });
    }

    //** Resources

    function getResources(projectId) {
      var promise = $http.get('/EasyAuthorPortal/api/GetResources/' + projectId, { cache: false });
      return promise.then(function (data) { return data.data; });
    }
    
    function getResource(resId) {
      var promise = $http.get('/EasyAuthorPortal/api/GetResource/' + resId);
      return promise.then(function (data) { return data.data; });
    }
    
    function getFile(resId) {
      var promise = $http.get('/EasyAuthorPortal/api/GetFile?resId=' + resId);
      return promise.then(function (data) { return data.data; });
    }
    
    function deleteFile(resId) {
      var promise = $http.get('/EasyAuthorPortal/api/DeleteFile?resId=' + resId);
      return promise.then(function (data) { return data.data; });
    }
    
    function uploadFile(resId) {
      var promise = $http.get('/EasyAuthorPortal/api/UploadFile?resId=' + resId);
      return promise.then(function (data) { return data.data; });
    }
    
    function addFolder(projectId, resId, isVolume) {
      var promise = $http.post('/EasyAuthorPortal/api/AddFolder', {
        projectId: projectId,
        parentId: resId,
        isVolume: isVolume
      });
      return promise.then(function (data) { return data.data; });
    }
    
    function emptyFolder(resId) {
      var promise = $http.get('/EasyAuthorPortal/api/EmptyFolder?resId=' + resId);
      return promise.then(function (data) { return data.data; });
    }
    
    function emptyTrash(projectId) {
      var promise = $http.get('/EasyAuthorPortal/api/EmptyTrash/' + projectId);
      return promise.then(function (data) { return data.data; });
    }
    
    function getSemantics(projectId) {
      var promise = $http.get('/EasyAuthorPortal/api/GetSemantics/' + projectId);
      return promise.then(function (data) { return data.data; });
    }

    function addTerm(locale, type, key, value) {
      var promise = $http.get('/EasyAuthorPortal/api/addTerm/?locale=' + locale + "&key=" + key + "&value=" + value + "&type=" + type);
      return promise.then(function (data) { return data.data; });
    }
    
    function changeTerm(id, type, key, value) {
      var promise = $http.get('/EasyAuthorPortal/api/addTerm/' + id + "?key=" + key + "&value=" + value + "&type=" + type);
      return promise.then(function (data) { return data.data; });
    }
    
    function deleteTerm(id) {
      var promise = $http.get('/EasyAuthorPortal/api/addTerm/' + id);
      return promise.then(function (data) { return data.data; });
    }

  }
})();